﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LEDStripMixer.Views.Editor.UserControls
{
    /// <summary>
    /// Interaction logic for ColourPicker.xaml
    /// </summary>
    public partial class ColourPicker : UserControl
    {
        #region Properties

        private Color selectedColour = Colors.Black;
        private Boolean IsMouseDown = false;
        private static double SwatchPositionX = 0;
        private static double SwatchPositionY = 0;

        public Color SelectedColour
        {
            get { return selectedColour; }
            private set
            {
                if (selectedColour != value)
                {
                    selectedColour = value;
                    UpdateTextBoxes();
                }
            }
        }

        #endregion

        public ColourPicker()
        {
            InitializeComponent();

            imgSwatch1.ConvertToBitmap("/Images/Swatch1.png");
            imgSwatch2.ConvertToBitmap("/Images/Swatch2.png");
            imgSwatch3.ConvertToBitmap("/Images/Swatch3.png");
        }

        #region Private Methods

        private void Swatch_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image img = (sender as Image);
            ColorImage.Source = img.Source;

            SelectedColour = GetPixelColourFromSwatchCoordinates(SwatchPositionX, SwatchPositionY);
        }

        private void CanvImage_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!IsMouseDown)
                return;

            try
            {
                SwatchPositionX = Mouse.GetPosition(CanvImage).X;
                SwatchPositionY = Mouse.GetPosition(CanvImage).Y;

                SelectedColour = GetPixelColourFromSwatchCoordinates(SwatchPositionX, SwatchPositionY);

                //Ok now, so update the mouse cursor position and the SelectedColor
                ellipsePixel.SetValue(Canvas.LeftProperty,
                                     (double)(SwatchPositionX - 5));
                ellipsePixel.SetValue(Canvas.TopProperty,
                                     (double)(SwatchPositionY - 5));
                CanvImage.InvalidateVisual();
                ellipsePixel.Visibility = System.Windows.Visibility.Visible;
            }
            catch (Exception ex)
            {
                //not much we can do
            }
        }

        private void UpdateTextBoxes()
        {
            txtRed.Text = SelectedColour.R.ToString();
            txtGreen.Text = SelectedColour.G.ToString();
            txtBlue.Text = SelectedColour.B.ToString();

            inkSelectedColour.Background = new SolidColorBrush(SelectedColour);
        }

        private void CanvImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown = true;
        }

        private void CanvImage_MouseUp(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown = false;
        }

        private Color GetPixelColourFromSwatchCoordinates(double x, double y)
        {
             CroppedBitmap cb = new CroppedBitmap(ColorImage.Source as BitmapSource,
                    new Int32Rect((int)x, (int)y, 1, 1));

                byte[] pixels = new byte[4];

                try
                {
                    cb.CopyPixels(pixels, 4, 0);
                }
                catch (Exception ex)
                {
                    //Ooops
                }

            return Color.FromRgb(pixels[2], pixels[1], pixels[0]);
        }

        #endregion
    }
}
